/**
 * UB RIA Base
 * Copyright 2013 Baidu Inc. All rights reserved.
 *
 * @file 为上传控件启用CSRF
 * @exports ui.extension.UploaderSessionToken
 * @author otakustay
 */
define(
    function (require) {
        /**
         * 为上传控件启用CSRF
         *
         * @class ui.extension.UploaderSessionToken
         * @extends esui.Extension
         */
        var exports = {};

        /**
         * 扩展的类型，始终为`"UploaderSessionToken"`
         *
         * @type {string}
         * @override
         */
        exports.type = 'UploaderSessionToken';

        /**
         * @override
         */
        exports.activate = function () {
            this.target.on('formcreate', this.addSessionToken, this);

            this.$super(arguments);
        };

        /**
         * @override
         */
        exports.inactivate = function () {
            this.target.un('formcreate', this.addSessionToken, this);

            this.$super(arguments);
        };

        /**
         * 重写默认属性
         *
         * @protected
         * @method ui.extension.UploaderSessionToken#UploaderSessionToken
         */
        exports.addSessionToken = function () {
            var uploader = this.target;
            uploader.setReadOnly(true);
            require('../../common/ioc').getComponent(
                'globalData',
                function (globalData) {
                    globalData.getUserProperty('sessionToken')
                        .thenBind(uploader.addFormField, uploader, 'sessionToken')
                        .thenBind(uploader.setReadOnly, uploader, false);
                }
            );
        };

        var Extension = require('esui/Extension');
        var UploaderSessionToken = require('eoo').create(Extension, exports);

        require('esui').registerExtension(UploaderSessionToken);

        return UploaderSessionToken;
    }
);
